from google.appengine.ext import db
from domain import article
from repository import articlerepository
from task import basetask

class ArticleTask(basetask.BaseTask):

    def __init__(self):
        basetask.BaseTask.__init__(self)
        self._has_next = False
        self._has_prev = False

    def hasNextPage(self):
        return self._has_next

    def hasPrevPage(self):
        return self._has_prev

    def getNewest(self, howmany):
        repository = articlerepository.ArticleRepository()
        articles = repository.getNewest(howmany)
        self.logmessage("ArticleTask retrieved new articles for main page, limit = " + str(howmany))
        return articles

    def getRSS(self, howmany):
        repository = articlerepository.ArticleRepository()
        articles = repository.getRange(0, howmany)
        self.logmessage("ArticleTask retrieved articles for RSS feed, limit = " + str(howmany))
        return articles

    def getAll(self):
        repository = articlerepository.ArticleRepository()
        articles = repository.getAll()
        self.logmessage("ArticleTask retrieved all articles, count = " + str(articles.count()))
        return articles

    def getAllByRange(self, page, items_per_page):
        repository = articlerepository.ArticleRepository()
        articles = repository.getAllByRange(page, items_per_page)
        self._has_next = repository.hasNext()
        self._has_prev = repository.hasPrev()
        self.logmessage("ArticleTask retrieved all articles by range, start = " + str(page) + ", count = " + str(items_per_page))
        return articles

    def getRange(self, page, items_per_page, category=None):
        repository = articlerepository.ArticleRepository()
        articles = repository.getRange(page, items_per_page, category)
        self._has_next = repository.hasNext()
        self._has_prev = repository.hasPrev()
        self.logmessage("ArticleTask retrieved published articles by range, page = " + str(page) + ", count = %s" + str(items_per_page))
        return articles

    def getByCategory(self, page, items_per_page, category):
        articles = self.getRange(page, items_per_page, category)
        self.logmessage("ArticleTask retrieved published articles for category: " + str(category))
        return articles

    def getMainFeature(self):
        repository = articlerepository.ArticleRepository()
        articles = repository.getMainFeature()
        self.logmessage("ArticleTask retrieved main feature article")
        return articles

    def getByKey(self, articlekey):
        thekey = db.Key(articlekey)
        repository = articlerepository.ArticleRepository()
        self.logmessage("ArticleTask retrieved article for key: " + str(articlekey))
        return repository.getByKey(thekey)

    def getByTitlePath(self, pathtosearch):
        repository = articlerepository.ArticleRepository()
        self.logmessage("ArticleTask retrieved article for title: " + str(pathtosearch))
        return repository.getByTitlePath(pathtosearch)

    def save(self, newarticle):
        if (isinstance(newarticle, article.Article)):
            repository = articlerepository.ArticleRepository()
            if (repository.save(newarticle)):
                self.logmessage("ArticleTask saved article: " + str(newarticle.title))
                return True
            else:
                self.logmessage("ArticleTask could not save article: " + str(newarticle.title))
                return False
        else:
            return False

    def delete(self, oldarticle):
        if (isinstance(oldarticle, article.Article)):
            repository = articlerepository.ArticleRepository()
            repository.delete(oldarticle)
            self.logmessage("Article deleted: " + str(oldarticle.title))
